<!--
 * 严肃声明：
 * 开源版本请务必保留此注释头信息，若删除我方将保留所有法律责任追究！
 * 本系统已申请软件著作权，受国家版权局知识产权以及国家计算机软件著作权保护！
 * 可正常分享和学习源码，不得用于违法犯罪活动，违者必究！
 * Copyright (c) 2020 陈尼克 all rights reserved.
 * 版权所有，侵权必究！
 *
-->

<template>
  <div class="product-list-wrap">
    <div class="product-list-content">
      <header class="category-header wrap">
        <i class="nbicon nbfanhui" @click="goBack"></i>
        <span style="margin: 0 auto; margin-left: 28%">已发布需求/商品</span>
      </header>
    </div>

    <div class="content">
      <van-pull-refresh
        v-model="state.refreshing"
        @refresh="onRefresh"
        class="product-list-refresh"
      >
        <van-list
          v-model:loading="state.loading"
          :finished="state.finished"
          :finished-text="state.productList.length ? '没有更多了' : '暂无信息'"
          @load="onLoad"
          @offset="10"
        >
          <!-- <p v-for="item in list" :key="item">{{ item }}</p> -->
          <template v-if="state.productList.length">
            <div
              class="product-item"
              v-for="(item, index) in state.productList"
              :key="index"
            >
              <img :src="$filters.prefix(item.cover_img)" />
              <div class="product-info">
                <p class="name">{{ item.title }}</p>
                <p class="subtitle">{{ item.company_name }}</p>
                <div class="btns">
                  <span v-if="item.state === 0">未审核</span>
                  <span v-else-if="item.state === 1">审核通过</span>
                  <span v-else>审核不通过</span>
                  <div>
                    <van-button plain round type="primary" @click="toEdit(item)"
                      >编辑</van-button
                    >
                    <van-button plain round type="primary" @click="del(item)"
                      >下架</van-button
                    >
                  </div>
                </div>
              </div>
            </div>
          </template>
          <img
            class="empty"
            v-else
            src="https://s.yezgea02.com/1604041313083/kesrtd.png"
            alt="搜索"
          />
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script setup>
import { reactive } from "vue";
import { useRoute, useRouter } from "vue-router";
import { getKeyword, getBy, deleteProduct } from "@/service/home";
import { getUserInfo } from "@/service/user";
import { showConfirmDialog, showSuccessToast } from "vant";
const route = useRoute();
const router = useRouter();
const state = reactive({
  keyword: route.query.keyword || "",
  searchBtn: false,
  seclectActive: false,
  refreshing: false,
  list: [],
  loading: false,
  finished: false,
  productList: [],
  totalPage: 0,
  page: 1,
  orderBy: "",
  queryParams: {
    salaryDown: "",
    salaryUp: "",
    city: "",
    parentId: "",
    categoryId: "",
    type: "",
    pageNum: 1,
    pageSize: 12,
    requirement: "",
  },
  show: false,
  user: null,
});

const init = async () => {
  const res = await getBy(
    state.user.id,
    state.queryParams.pageNum,
    state.queryParams.pageSize
  );

  state.productList = state.productList.concat(res.items);
  state.totalPage = Math.ceil(res.total / state.queryParams.pageSize);
  state.loading = false;
  if (state.queryParams.pageNum >= state.totalPage) state.finished = true;
};

const goBack = () => {
  router.go(-1);
};

const productDetail = (item) => {
  router.push({ path: `/product/${item.id}` });
};

const del = async (item) => {
  showConfirmDialog({
    title: "确认",
    message: "确认删除该条信息？",
  })
    .then(() => {
      deleteProduct(item.id).then((res) => {
        if (res.code == 2000) {
          showSuccessToast("删除成功");
          onRefresh();
        }
      });
    })
    .catch(() => {
      // on cancel
    });
};

const toEdit = async (item) => {
  router.push({ path: "/edit", query: { item: JSON.stringify(item) } });
};

const onLoad = async () => {
  const data = await getUserInfo();
  state.user = data;

  if (!state.refreshing && state.page < state.totalPage) {
    state.queryParams.pageNum = state.queryParams.pageNum + 1;
  }
  if (state.refreshing) {
    state.productList = [];
    state.refreshing = false;
  }
  init();
};

const onRefresh = () => {
  state.refreshing = true;
  state.finished = false;
  state.loading = true;
  state.page = 1;
  onLoad();
};
</script>

<style lang="less" scoped>
@import "../common/style/mixin";
.product-list-content {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  z-index: 1000;
  background: #fff;
  .category-header {
    .fj();
    width: 100%;
    height: 50px;
    line-height: 50px;
    padding: 0 15px;
    .boxSizing();
    font-size: 15px;
    color: #656771;
    z-index: 10000;
    &.active {
      background: @primary;
    }
    .icon-left {
      font-size: 25px;
      font-weight: bold;
    }
    .header-search {
      display: flex;
      width: 76%;
      line-height: 20px;
      margin: 10px 0;
      padding: 5px 0;
      color: #232326;
      background: #f7f7f7;
      .borderRadius(20px);
      .nbSearch {
        padding: 0 5px 0 20px;
        font-size: 17px;
      }
      .search-title {
        font-size: 12px;
        color: #666;
        background: #f7f7f7;
      }
    }
    .icon-More {
      font-size: 20px;
    }
    .search-btn {
      height: 28px;
      margin: 8px 0;
      line-height: 28px;
      padding: 0 5px;
      color: #fff;
      background: @primary;
      .borderRadius(5px);
      margin-top: 10px;
    }
  }
}
.content {
  height: calc(~"(100vh - 70px)");
  overflow: hidden;
  overflow-y: scroll;
  margin-top: 78px;
}
.product-list-refresh {
  .product-item {
    .fj();
    width: 100%;
    height: 120px;
    padding: 10px 0;
    border-bottom: 1px solid #dcdcdc;
    img {
      width: 140px;
      height: 100px;
      padding: 0 10px;
      .boxSizing();
    }
    .product-info {
      width: 56%;
      height: 120px;
      padding: 5px;
      text-align: left;

      .btns {
        padding-top: 20px;
        display: flex;
        justify-content: space-between;

        span {
          line-height: 41px;
        }
      }

      .boxSizing();
      p {
        margin: 0;
      }
      .name {
        width: 100%;
        max-height: 20px;
        line-height: 20px;
        font-size: 15px;
        color: #333;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .subtitle {
        width: 100%;
        max-height: 40px;
        padding: 10px 0;
        line-height: 25px;
        font-size: 13px;
        color: #999;
        overflow: hidden;
      }
      .price {
        color: @primary;
        font-size: 16px;
      }
    }
  }
  .empty {
    display: block;
    width: 150px;
    margin: 50px auto 20px;
  }
}
</style>
